using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel.Composition;

namespace Yammu.Core.VirtualFileSystem 
{   
 
    /// <summary>
    /// Represents a local directory
    /// </summary>
	public  sealed class LocalVirtualDirectory : VFSDirectoryBase
	{
	    private string _physicalDir;

        /// <summary>
        /// Initializes a new instance of the <see cref="LocalVirtualDirectory"/> class. If the value of virtualPath is null then the children
        /// directories of this will be the list of drives
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
		internal LocalVirtualDirectory (string virtualPath)
			: base (virtualPath)
		{
		    _physicalDir = VirtualPath;

		}

        List<IVFSItem> AddDirectoriesToList(List<IVFSItem> list)
		{
			
			
            
			foreach (string path in Directory.GetDirectories (_physicalDir))
				list.Add (new LocalVirtualDirectory (path));

			return list;
		}

        List<IVFSItem> AddDrivesToList(List<IVFSItem> list)
        {

            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach(var driveInfo in drives)
            {
                list.Add(new LocalVirtualDirectory((driveInfo.RootDirectory.Name)));
            }
            return list;
        }


     
        List<IVFSItem> AddFilesToList(List<IVFSItem> list)
		{
			
			
			foreach (string path in Directory.GetFiles (_physicalDir))
				list.Add (new LocalVirtualFile (path));

			return list;
		}

        public override IList<IVFSItem> Children
        {
			get 
            {
                if (null == _physicalDir)
                {
                    return AddDrivesToList(new List<IVFSItem>()).AsReadOnly();
                }
                var list = new List<IVFSItem>();
				AddDirectoriesToList (list);
				return AddFilesToList (list).AsReadOnly();
			}
		}

        public override IList<IVFSItem> Directories
        {
			get
            {
                if( null == _physicalDir)
                {
                    return AddDrivesToList(new List<IVFSItem>()).AsReadOnly();
                }
                return AddDirectoriesToList(new List<IVFSItem>()).AsReadOnly();
			}
		}

        public override IList<IVFSItem> Files
        {
			get {
                return AddFilesToList(new List<IVFSItem>()).AsReadOnly();
			}
		}

       
	}
}


